﻿using Microsoft.EntityFrameworkCore.Query;
using System.Linq.Expressions;
using Yz.Base;
using Yz.DB.EFCore.Extensions;

namespace Yz.DB.EFCore.Dal
{
    public class DbAccess<TEntity> : IDbAccess<TEntity> where TEntity : BaseEntity
    {
        public readonly YzContext _yzContext;
        public DbAccess(YzContext yzContext)
        {
            _yzContext = yzContext;
        }

        #region 新增
        public ReturnCode Add(TEntity model, Expression<Func<TEntity, bool>> where = null)
        {

            return _yzContext.Add(model, where);
        }
        public async Task<ReturnCode> AddAsync(TEntity model, Expression<Func<TEntity, bool>> where = null)
        {

            return await _yzContext.AddAsync(model, where);
        }
        public ReturnCode Add(TEntity model, List<Expression<Func<TEntity, bool>>> whereList)
        {

            return _yzContext.Add(model, whereList);
        }
        public async Task<ReturnCode> AddAsync(TEntity model, List<Expression<Func<TEntity, bool>>> whereList)
        {

            return await _yzContext.AddAsync(model, whereList);
        }
        public TEntity AddGet(TEntity model, Expression<Func<TEntity, bool>> where = null)
        {

            return _yzContext.AddGet(model, where);
        }
        public async Task<TEntity> AddGetAsync(TEntity model, Expression<Func<TEntity, bool>> where = null)
        {

            return await _yzContext.AddGetAsync(model, where);
        }
        public TEntity AddGet(TEntity model, List<Expression<Func<TEntity, bool>>> whereList)
        {

            return _yzContext.AddGet(model, whereList);
        }
        public async Task<TEntity> AddGetAsync(TEntity model, List<Expression<Func<TEntity, bool>>> whereList)
        {

            return await _yzContext.AddGetAsync(model, whereList);
        }
        public int BatchAdd(List<TEntity> modelList)
        {

            return _yzContext.BatchAdd(modelList);
        }
        public async Task<int> BatchAddAsync(List<TEntity> modelList)
        {

            return await _yzContext.BatchAddAsync(modelList);
        }
        #endregion

        #region 修改
        public ReturnCode Update(Expression<Func<SetPropertyCalls<TEntity>, SetPropertyCalls<TEntity>>> setPropertyCalls, Expression<Func<TEntity, bool>> where1 = null, Expression<Func<TEntity, bool>> where2 = null)
        {

            return _yzContext.Update(setPropertyCalls, where1, where2);
        }
        public async Task<ReturnCode> UpdateAsync(Expression<Func<SetPropertyCalls<TEntity>, SetPropertyCalls<TEntity>>> setPropertyCalls, Expression<Func<TEntity, bool>> where1 = null, Expression<Func<TEntity, bool>> where2 = null)
        {

            return await _yzContext.UpdateAsync(setPropertyCalls, where1, where2);
        }
        public ReturnCode Update(Expression<Func<SetPropertyCalls<TEntity>, SetPropertyCalls<TEntity>>> setPropertyCalls, List<Expression<Func<TEntity, bool>>> whereList2, Expression<Func<TEntity, bool>> where1 = null)
        {

            return _yzContext.Update(setPropertyCalls, whereList2, where1);
        }
        public async Task<ReturnCode> UpdateAsync(Expression<Func<SetPropertyCalls<TEntity>, SetPropertyCalls<TEntity>>> setPropertyCalls, List<Expression<Func<TEntity, bool>>> whereList2, Expression<Func<TEntity, bool>> where1 = null)
        {

            return await _yzContext.UpdateAsync(setPropertyCalls, whereList2, where1);
        }
        public int BatchUpdate(Expression<Func<TEntity, TEntity>> epsUpdate, List<TEntity> modelList)
        {

            return _yzContext.BatchUpdate(epsUpdate, modelList);
        }
        public async Task<int> BatchUpdateAsync(Expression<Func<TEntity, TEntity>> epsUpdate, List<TEntity> modelList)
        {

            return await _yzContext.BatchUpdateAsync(epsUpdate, modelList);
        }
        public int BatchUpdate(Expression<Func<SetPropertyCalls<TEntity>, SetPropertyCalls<TEntity>>> setPropertyCalls, Expression<Func<TEntity, bool>> where = null)
        {

            return _yzContext.BatchUpdate(setPropertyCalls, where);
        }
        public async Task<int> BatchUpdateAsync(Expression<Func<SetPropertyCalls<TEntity>, SetPropertyCalls<TEntity>>> setPropertyCalls, Expression<Func<TEntity, bool>> where = null)
        {

            return await _yzContext.BatchUpdateAsync(setPropertyCalls, where);
        }
        #endregion

        #region 删除
        public ReturnCode Delete(Expression<Func<TEntity, bool>> where = null)
        {

            return _yzContext.Delete(where);
        }
        public async Task<ReturnCode> DeleteAsync(Expression<Func<TEntity, bool>> where)
        {

            return await _yzContext.DeleteAsync(where);
        }
        public int BatchDelete(Expression<Func<TEntity, bool>> where = null)
        {

            return _yzContext.BatchDelete(where);
        }
        public async Task<int> BatchDeleteAsync(Expression<Func<TEntity, bool>> where = null)
        {

            return await _yzContext.BatchDeleteAsync(where);
        }
        #endregion

        #region 查询 
        /// <summary>
        /// 根据条件查询返回IQueryable<TEntity>
        /// </summary>
        /// <typeparam name="TEntity"></typeparam>
        /// <param name="db"></param>
        /// <param name="where"></param>
        /// <returns></returns>
        public IQueryable<TEntity> GetQueryable(Expression<Func<TEntity, bool>> where)
        {
            return _yzContext.GetQueryable(where);
        }
        public int GetTotal(Expression<Func<TEntity, bool>> where = null)
        {

            return _yzContext.GetTotal(where);
        }
        public async Task<int> GetTotalAsync(Expression<Func<TEntity, bool>> where = null)
        {

            return await _yzContext.GetTotalAsync(where);
        }
        public ReturnList<T> GetListByPage<T>(Expression<Func<TEntity, bool>> where = null, ParaPage paraPage = null, List<ParaOrder<TEntity>> orders = null, bool hasTotal = true) where T : ViewModel
        {

            return _yzContext.GetListByPage<TEntity, T>(where, paraPage, orders, hasTotal);
        }
        public async Task<ReturnList<T>> GetListByPageAsync<T>(Expression<Func<TEntity, bool>> where = null, ParaPage paraPage = null, List<ParaOrder<TEntity>> orders = null, bool hasTotal = true) where T : ViewModel
        {

            return await _yzContext.GetListByPageAsync<TEntity, T>(where, paraPage, orders, hasTotal);
        }
        public T GetModel<T>(Expression<Func<TEntity, bool>> where = null, List<ParaOrder<TEntity>> orders = null) where T : ViewModel
        {

            return _yzContext.GetModel<TEntity, T>(where, orders);
        }
        public async Task<T> GetModelAsync<T>(Expression<Func<TEntity, bool>> where = null, List<ParaOrder<TEntity>> orders = null) where T : ViewModel
        {

            return await _yzContext.GetModelAsync<TEntity, T>(where, orders);
        }
        public ReturnList<T> GetUnionListByPage<T>(List<Expression<Func<TEntity, bool>>> whereList, ParaPage paraPage = null, List<ParaOrder<TEntity>> orders = null, bool hasTotal = true, bool isOnly = true) where T : ViewModel
        {

            return _yzContext.GetUnionListByPage<TEntity, T>(whereList, paraPage, orders, hasTotal, isOnly);
        }
        public async Task<ReturnList<T>> GetUnionListByPageAsync<T>(List<Expression<Func<TEntity, bool>>> whereList, ParaPage paraPage = null, List<ParaOrder<TEntity>> orders = null, bool hasTotal = true, bool isOnly = true) where T : ViewModel
        {

            return await _yzContext.GetUnionListByPageAsync<TEntity, T>(whereList, paraPage, orders, hasTotal, isOnly);
        }
        public int GetUnionTotal(List<Expression<Func<TEntity, bool>>> whereList)
        {

            return _yzContext.GetUnionTotal(whereList);
        }
        public async Task<int> GetUnionTotalAsync(List<Expression<Func<TEntity, bool>>> whereList)
        {

            return await _yzContext.GetUnionTotalAsync(whereList);
        }
        public bool IsExist(Expression<Func<TEntity, bool>> where = null)
        {

            return _yzContext.IsExist(where);
        }
        public async Task<bool> IsExistAsync(Expression<Func<TEntity, bool>> where = null)
        {

            return await _yzContext.IsExistAsync(where);
        }
        #endregion
    }
}
